
"""
BeautifulSoup(response.text, 'html.parser') 用于解析HTML内容.
soup.title.text 返回网页的标题.
"""

import requests
from bs4 import BeautifulSoup

url = 'https://www.baidu.com'
response = requests.get(url)
response.encoding = response.apparent_encoding  # 自动检测编码格式
print(response.text)

soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.text
print('网页标题:', title)
# 获取所有的<a>标签
links = soup.find_all('a')

# 打印所有链接
for link in links:
    href = link.get('href')
    print(href)
"""
soup.find_all('a') 用于获取网页中的所有链接.
link.get('href') 获取每个链接的 href 属性,也就是网址.
"""